<template>
  <div class="padding-l-20 padding-r-20 padding-t-20 h-100 table-page">
    <filter-bar>
      <template slot="right-bar">
        <a-button class="nbcs-btn nbcs-btn-primary position-r" @click="edit(0)">新增</a-button>
        <!-- <a-upload name="file" :multiple="true"  action="/ledWeb/file/upload" :headers="headers" @change="handleChange">
          <a-button class="nbcs-btn nbcs-btn-primary position-r"> <a-icon :type="uploading ? 'loading' : 'upload'" />上传apk包</a-button>
        </a-upload> -->
      </template>
    </filter-bar>

    <a-table
      ref="table"
      :columns="columns"
      :data-source="list"
      :pagination="pageConfig"
      :page="page"
      rowKey="id"
      :loading="loading"
      :scroll="{ x: 1000, y: 720 }"
      @showSizeChange="onSizeChange"
      @change="onChange"
    >
      <span slot="action" slot-scope="text, record">
        <a-popconfirm title="确定删除该升级包吗？" @confirm="del(record.id)">
          <a-icon slot="icon" type="question-circle-o" style="color: red" />
          <a class="table-red-btn">删除</a>
        </a-popconfirm>
      </span>
    </a-table>

    <software-edit-form ref="SoftwareEditForm" @getList="getList(page, searchForm)"></software-edit-form>
  </div>
</template>


<script>
import Cookie from 'js-cookie';
import FilterBar from '@/layouts/FilterBar.vue';
import { list, del } from '@/services/software';
import SoftwareEditForm from './softwareEditForm.vue';

export default {
  name: 'User',
  components: { FilterBar,SoftwareEditForm },
  data() {
    return {
      loading: false,
      showSoftwareEdit:false,
      orgArr: [],
      headers: {
        Authorization: Cookie.get('Authorization'),
      },
      scrollY: 0,
      uploading: false,
      enabledArr: [
        { title: '账号状态（全部）', key: '' },
        { title: '正常', key: 1 },
        { title: '锁定', key: 0 },
      ],
      searchForm: {
        realName: '',
        username: '',
        enabled: '',
      },
      list: [],
      columns: [
        { title: '#', width: 60, dataIndex: 'id', key: '#' },
        { title: '升级包名', dataIndex: 'fileName', ellipsis: true },
        { title: '版本号', dataIndex: 'fileVersion', ellipsis: true },
        { title: '上传时间', dataIndex: 'updateTime' },
        {
          title: '操作账号',
          dataIndex: 'createUserName',
        },
        {
          title: '文件大小',
          dataIndex: 'fileSize',
        },
        {
          title: '应用设备数量',
          dataIndex: 'useCount',
          customRender:(text)=>{
            if(!text) {return 0}
            else{return text}
          }
        },
        {
          title: '操作',
          key: 'operation',
          width: 170,
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageConfig: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultPageSize: 20,
        pageSizeOptions: ['20', '30', '50'],
        total: 0,
        showTotal: (total) => {
          return `共 ${total} 条`;
        },
      },
      page: {
        currentPage: 1,
        pageSize: 20,
      },
    };
  },
  computed: {},
  created() {
    this.getList(this.page);
  },
  methods: {
    getList(page, params) {
      this.loading = true;
      list(
        Object.assign(
          {
            page: page.currentPage,
            pageSize: page.pageSize,
          },
          params
        )
      ).then((res) => {
        this.loading = false;
        const { data } = res.data;
        this.list = data.list;
        this.pageConfig.total = data.totalRow;
      });
    },
    del(id) {
      del({id:id}).then((res) => {
        if (res.data.code === 0) {
          this.$message.success('删除成功');
          this.getList(this.page);
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    beforeUpload(file){
      console.log(file.type);
      if(file.type != 'application/vnd.android.package-archive'){
        this.$message.error('当前上传文件格式错误，请选择apk格式进行上传');
        return false;
      }
      return true;
    },
    handleChange(info) {
      if (info.file.status === 'uploading') {
        this.uploading = true;
        this.$message.loading("文件上传中，保持在当前页面，请勿进行操作");
      }
      if (info.file.status === 'done' && info.file.response.code == 0) {
        this.$message.success(`${info.file.name} 上传成功`);
        this.uploading = false;
        this.getList(this.page);
      } else if (info.file.status === 'error') {
        this.uploading = false;
        this.$message.error(`${info.file.name} 上传失败`);
        this.getList(this.page);
      }
    },
    onChange(page) {
      this.page.currentPage = page.current;
      this.page.pageSize = page.pageSize;
      this.getList(this.page);
    },
    onSizeChange(data) {
      this.page.currentPage = data.page;
      this.page.pageSize = data.pageSize;
      this.getList(this.page);
    },
    edit(action, data = {}) {
      this.$refs['SoftwareEditForm'].changeForm(true, action, data);
    },
  },
};
</script>

<style lang="less" scoped>
</style>
